
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.tongtech.business.dao.sys_image_view.ImageVierRepository;
import com.tongtech.business.dao.sys_image_view.ImageView;
import com.tongtech.common.vo.RestResult;
import com.tongtech.common.vo.RestResultFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 转换后放在数据库


    public void convertWordToSingleImage(String wordFilePath, String monthTime) throws Exception {
        Document doc = new Document(wordFilePath);
        // 获取文档页数
        int pageCount = doc.getPageCount();
        // 循环遍历每一页并保存为图片
        List<ImageView> imageViewList = new ArrayList<>();
        for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
            Document extractedPage = doc.extractPages(pageIndex, 1);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            extractedPage.save(outputStream, SaveFormat.PNG);
            byte[] documentData = outputStream.toByteArray();
            ImageView imageView = new ImageView();
            // 将图片字节添加到列表中
            imageView.setImageName(monthTime + "月度报告" + LocalDateTime.now() + ".png");
            imageView.setImageType("月度报告");
            imageView.setMonthTime(monthTime);
            imageView.setCreateDate(LocalDate.now());
            imageView.setImageData(documentData);
            imageViewList.add(imageView);
        }
        imageVierRepository.saveAll(imageViewList);
    }


    // 转换后存放在磁盘
     @Test
    public void test1() throws Exception {
        File file = new File("/Users/chipingyung/IdeaProjects/xz/extra-statindic/src/main/resources/月报模板.docx");

//        String pdfFilePath = "/Users/chipingyung/IdeaProjects/xz/extra-statindic/src/main/resources/" + file.getName().replace(".docx", ".pdf");
        String pdfFilePath = "/Users/chipingyung/IdeaProjects/xz/extra-statindic/src/main/resources";
        convertWordToSingleImage("/Users/chipingyung/IdeaProjects/xz/extra-statindic/src/main/resources/月报模板.docx", pdfFilePath);

    }

    public static void convertWordToSingleImage(String wordFilePath, String outputImagePath) throws Exception {
        Document doc = new Document(wordFilePath);

        // 设置输出路径
        String outPath = outputImagePath;
        // 创建输出目录
        File outputDirectory = new File(outPath);
        if (!outputDirectory.exists()) {
            outputDirectory.mkdirs();
        }
        // 获取文档页数
        int pageCount = doc.getPageCount();
//         存储所有页面的图片
        ArrayList<Object> images = new ArrayList<>();
//         循环遍历每一页并保存为图片
        for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
            Document extractedPage = doc.extractPages(pageIndex, 1);
            // 设置每一页的保存路径
            String pageOutPath = outPath + "page_" + new Date().getTime() + "_" + (pageIndex + 1) + ".png";
            // 保存为图片
            extractedPage.save(pageOutPath, SaveFormat.PNG);
            // 加载图片到内存
            BufferedImage image = ImageIO.read(new File(pageOutPath));
            images.add(image);
        }
        System.out.println("Word to Image conversion completed.");

    }